//
//  TopScrollView.m
//  kyMusicPlayer
//
//  Created by 翁科宇 on 2018/5/11.
//  Copyright © 2018年 翁科宇. All rights reserved.
//

#import "TopScrollView.h"
static CGFloat kTitleLabelNorimalFont = 13;
static CGFloat kTitleLabelSelectedFont = 16;
//static CGFloat kAddChannelWidth = 30;
//static CGFloat kSliderViewWidth = 20;
static CGFloat buttonWidth = 70;

@interface TopScrollView()
@property(nonatomic, strong)UIButton *lastSelectedBtn;
@property(nonatomic, strong)UIView *indicatorView;

@end


@implementation TopScrollView
@synthesize channelNameArray = _channelNameArray;
#pragma mark 初始化TopScrollView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0.9;
        //初始化ScrollView
        UIScrollView *scrollView = [self createScrollView];
        self.scrollView = scrollView;
        [self addSubview:self.scrollView];
        //初始化被选中channelButton的红线(效果不对,暂时addSubView隐藏)
        UIView *indicatorView = [self createIndicatorView];
        self.indicatorView = indicatorView;
        //[self.scrollView bringSubviewToFront:self.indicatorView];
        [self.scrollView addSubview:self.indicatorView];
    }
    return self;
}
#pragma mark 创建容纳channelButton的ScrollView
- (UIScrollView *)createScrollView{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 0, ScreenWith, self.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    return scrollView;
}
#pragma mark 创建被选中channelButton的红线，也就是indicatorView
- (UIView *)createIndicatorView{
    UIView *indictorView = [[UIView alloc] init];
    indictorView.backgroundColor = RGBA(243, 75, 80, 1.0);
    [self addSubview:indictorView];
    return indictorView;
}

//channelNameArray的setter方法
- (void)setChannelNameArray:(NSArray *)channelNameArray{
    _channelNameArray = channelNameArray;
    self.scrollView.contentSize = CGSizeMake(buttonWidth *channelNameArray.count, 0);//表示只能只能水平滚动(不允许垂直滚动)
    for (NSInteger i = 0; i < channelNameArray.count; i++) {
        UIButton *button = [self createChannelButton];
        button.frame = CGRectMake(i * buttonWidth, 0, buttonWidth, self.frame.size.height);
        [button setTitle:channelNameArray[i] forState:UIControlStateNormal];
        [self.scrollView addSubview:button];
    }
    //默认选中第一个ChannelButton
    [self clickChannelBtn:self.scrollView.subviews[1]];
}

- (NSArray *)channelNameArray{
    if (_channelNameArray == nil) {
        _channelNameArray = [[NSArray alloc] init];
    }
    return _channelNameArray;
}

#pragma mark 创建ChannelButton
- (UIButton *)createChannelButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor whiteColor]];
    [button setTitleColor:RGBA(243, 75, 80, 1.0) forState:UIControlStateDisabled];
    button.titleLabel.font = [UIFont systemFontOfSize:kTitleLabelNorimalFont];
    [button addTarget:self action:@selector(clickChannelBtn:) forControlEvents:UIControlEventTouchUpInside];
    [button layoutIfNeeded];
    return button;
}
//选择了某个ChannelButton(更改字体颜色等)
- (void)clickChannelBtn:(UIButton *)sender{
    self.lastSelectedBtn.titleLabel.font = [UIFont systemFontOfSize:kTitleLabelNorimalFont];
    self.lastSelectedBtn.enabled = YES;
    self.lastSelectedBtn = sender;
    self.lastSelectedBtn.enabled = NO;
    //选中的标签要居中,也就是scrollView的offset.x加屏幕的一半要等于标签的中心
    CGFloat newOffsetX = sender.center.x - ScreenWith *0.5;
    if (newOffsetX < 0) {
        newOffsetX = 0;
    }
    if (newOffsetX > self.scrollView.contentSize.width - self.scrollView.frame.size.width) {
        newOffsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
    }
    [UIView animateWithDuration:0.25 animations:^{
        sender.titleLabel.font = [UIFont systemFontOfSize:kTitleLabelSelectedFont];
        [sender layoutIfNeeded];
        self.scrollView.contentOffset = CGPointMake(newOffsetX, 0);
        //indicatorView宽度会比titleLabel宽20，centerX与titleLabel相同
        self.indicatorView.frame = CGRectMake(sender.frame.origin.x + sender.titleLabel.frame.origin.x - 10, self.frame.size.height-2, sender.titleLabel.frame.size.width+20, 2);

    }];
    //因为subviews包含indicatorView,所以index需要减1
    NSInteger index = [self.scrollView.subviews indexOfObject:sender] - 1;
    if ([self.delegate respondsToSelector:@selector(chooseChannelWithIndex:)]) {
        [self.delegate chooseChannelWithIndex:index];
    }
}

#pragma mark 选中某个ChannelButton
- (void)selectChannelButtonWithIndex:(NSInteger)index{
    self.indicatorView.hidden = NO;
    //因为subviews包含indicatorView,所以index需要加1
    [self clickChannelBtn:self.scrollView.subviews[index + 1]];
}

#pragma mark 删除某个ChannelButton
- (void)deleteChannelButtonWithIndex:(NSInteger)index{
//    //删除index对应的button，因为subviews包含indicatorView,所以index需要加1
//    NSInteger realIndex = index + 1;
//    [self.scrollView.subviews[realIndex] removeFromSuperview];
//    //后面的button的x向左移动buuton宽度的距离
//    for (NSInteger i = realIndex; i < self.scrollView.subviews.count; i++) {
//        UIButton *button = self.scrollView.subviews[i];
//        CGRect buttonFrame = button.frame;
//        button.frame = CGRectMake(buttonFrame.origin.x - button.frame.size.width, buttonFrame.origin.y, buttonFrame.size.width, buttonFrame.size.height);
//
//    }
//    //将scrollView的contentSize减小一个buuton的宽度
//     self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width - self.scrollView.frame.size.width/5, 0);
}


#pragma mark 添加新闻频道：增加scrollView的contensize，然后在最后添加一个channelButton
- (void)addChannelButtonWithChannelName:(NSString *)channelName {
//    UIButton *button = [self createChannelButton];
//    self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width + buttonWidth, 0);
//    button.frame = CGRectMake(self.scrollView.contentSize.width - buttonWidth , 0, buttonWidth, self.frame.size.height);
//    [button setTitle:channelName forState:UIControlStateNormal];
//    [self.scrollView addSubview:button];
}






@end
